# -*- coding: utf-8 -*-
"""
Created on Tue Jun 09 09:04:44 2019

@author: blaslau
"""

import numpy as np
import pandas as pd

from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.tree import DecisionTreeClassifier

state = list()

# status of the experiments
result = pd.read_csv('../dataset/train.csv')

# iterate through each experiment 1 to 19
# based on train data, concatenate all the experiment in one, appending the status
# of the tool named Damaged
for i in range(1,19):
    experimentNumber = '0'
    if i > 9:
        experimentNumber = str(i)
    else:
        experimentNumber = '0' + str(i)
        
    # print(experimentNumber)
    
    experiment = pd.read_csv('../dataset/experiment_{0}.csv'.format(experimentNumber))
    # Get the current number
    row = result.loc[result['No'] == i]
    
    # Set the tool condition
    # 1 - damaged, 0 - not damaged
    if row.iloc[0]['tool_condition'] == 'worn':
        experiment['Damaged'] = 1
    else:
        experiment['Damaged'] = 0
        
    state.append(experiment)
    
# for ends

# concatenate
dataFrame = pd.concat(state, ignore_index=True, sort=False)

# Print first 5 rows - meh
# print(dataFrame.head())

# Assign ordinal levels to categorical data (Machining_Process)
# Split the data into train and test sets
# 80% train size
le = LabelEncoder()
le.fit(dataFrame['Machining_Process'])
dataFrame['Machining_Process'] = le.transform(dataFrame['Machining_Process'])

y = np.array(dataFrame['Damaged'])
x = dataFrame.drop('Damaged', axis=1).values

# train
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8, test_size=0.2, random_state=101)

# now using DecisionTree (arbore de decizie) as model
model = DecisionTreeClassifier()
model.fit(x_train, y_train)

# - I guess at some point I have to export the model -

y_score = model.predict(x_test)

# to be done some plotting and stats
    
    